package com.app.service.impl;

import com.app.mapper.jiuquan.LoanInfoMapper;
import com.app.model.LoanInfoModel;
import com.app.model.TbcConfigModel;
import com.app.service.LoanInfoService;
import com.app.service.TbcConfigService;
import com.app.util.StringTool;
import com.app.vo.LoanInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

//贷款信息
@Service
public class LoanInfoServiceImpl extends ServiceImpl<LoanInfoMapper, LoanInfoModel> implements LoanInfoService {

    @Autowired
    private LoanInfoMapper loanInfoMapper;

    @Override
    public LoanInfoVo getLoanInfoList(Integer page, Integer limit, String title, Double maxMoney) {
        Page<LoanInfoModel> page1 = new Page<>(page, limit);
        QueryWrapper<LoanInfoModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(title)) {
            qw.like("title", title);
        }
        if (maxMoney != null) {
            qw.le("max_money", maxMoney);
        }
        Page<LoanInfoModel> loanInfoPage = loanInfoMapper.selectPage(page1, qw);
        List<LoanInfoModel> loanInfoList = loanInfoPage.getRecords();
//        for (LoanInfoModel loanInfo : loanInfoList) {
//            Double maxMoneyOld = loanInfo.getMaxMoney();
//            String  strMaxMoney = String.format("%.2f",maxMoneyOld);
//            Double setMaxMoney = Double.parseDouble(strMaxMoney);
//            loanInfo.setMaxMoney(setMaxMoney);
//        }
        long count = loanInfoPage.getTotal();

        LoanInfoVo loanInfoVo = new LoanInfoVo();
        loanInfoVo.setList(loanInfoList);
        loanInfoVo.setCount((int) count);
        return loanInfoVo;
    }

    @Override
    public LoanInfoModel getLoanInfoByUser(String openId) {
        QueryWrapper<LoanInfoModel> qw = new QueryWrapper<>();
        qw.eq("open_id", openId);
        LoanInfoModel loanInfo = loanInfoMapper.selectOne(qw);
        return loanInfo;
    }
}
